

import { watch, ref } from 'vue'

export const uploadCommon = {
    setup() {
        const { proxy } = getCurrentInstance();
        const IMGStatus = ref(0);//状态为0需要上传
        const formList = ref("");
        let ImgFiles = []
        function UploadSuccess(url) {
            IMGStatus.value = 1
            console.log('上传成功', url)
            let imgs = []
            for (let i in url) {
                imgs.push(url[i].url)
            }
            formList.value = imgs.join(',')
        }
        function RemoveSuccess(url) {//删除图片
            formList.value = ''
        }
        function isChange(file) {
            // console.log('图片改变', file)
            let imgs = []
            for (let i in file) {
                imgs.push(file[i].url)
            }
            ImgFiles = imgs
            console.log('图片改变', ImgFiles)
        }
        let formValue = undefined;
        function submit(val) {//提交
            formValue = val
            console.log('formList.photo====', formList.value)
            let ImgPhoto = ImgFiles.join(',')
            //   console.log('ImgPhoto', ImgPhoto)
            if (ImgPhoto == "") {
                IMGStatus.value = 1;

            } else if (formList.value != "" && formList.value != null) {
                IMGStatus.value = 1;
            }
            else {
                proxy.$refs['upload'].submitUpload();

            }
        }
        watch(() => IMGStatus.value, () => {
            if (IMGStatus.value == 1) {
                formValue(formList.value);

            }
        }, { deep: true, immediate: true });
        return {
            formList,
            IMGStatus,
            UploadSuccess,
            RemoveSuccess,
            submit,
            isChange
        }
    },



}